package com.itheima.health.controller;


import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.util.SMSUtilsMarket;
import com.itheima.health.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("mobile/validateCode")
@Slf4j
public class SMSController {
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/send")
    public Result send( String type, String telephone) {
        log.info("-------控制台输出type-------,{}", type);
        log.info("-------控制台输出type-------,{}", telephone);

        //判断手机号
        if(telephone==null){
            throw new BusinessRuntimeException("手机号不能为空");
        } else if (!(telephone.length()>0&&telephone.length()<=11)){
            throw new BusinessRuntimeException("手机号输入有误.请检查");
        }
        //生成验证码
        String code = ValidateCodeUtils.generateValidateCode(6).toString();

//使用阿里云的短信验证
    //    SMSUtilsMarket.sendMessage(telephone,code,"2e65b1bb3d054466b82f0c9d125465e2","908e94ccf08b4476ba6c876d13f084ad","fa817e8461ff40dd9c797658f3dc37a1");
/*        //调用阿里云提供的短信服务API完成发送短信
        SMSUtils.sendMessage("瑞吉外卖","",phone,code);*/
        //存入redis中
        redisTemplate.opsForValue().set(telephone, code, 3, TimeUnit.MINUTES);
        log.info("-------控制台输出code-------,{}", code);
        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }
}
